/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#define GLCLIENT
#include "gl_internal.h"

void glOrtho(GLdouble l, GLdouble r, GLdouble b, GLdouble t, GLdouble n, GLdouble f) {
    /*TODO: add error checking */
    Matrix m;

    Fixed fl = Fixed_fromFloat(l);
    //Fixed nfl = Fixed_neg(fl);
    Fixed fr = Fixed_fromFloat(r);
    Fixed nfr = Fixed_neg(fr);
    Fixed fb = Fixed_fromFloat(b);
    //Fixed nfb = Fixed_neg(fb);
    Fixed ft = Fixed_fromFloat(t);
    Fixed nft = Fixed_neg(ft);
    Fixed fn = Fixed_fromFloat(n);
    //Fixed nfn = Fixed_neg(fn);
    Fixed ff = Fixed_fromFloat(f);
    Fixed nff = Fixed_neg(ff);

    Fixed tx = Fixed_sub(nfr, fl);
    Fixed ty = Fixed_sub(nft, fb);
    Fixed tz = Fixed_sub(nff, fn);

    Fixed zero = Fixed_zero;

    m.col[0][0] = Fixed_sub(fr, fl);  m.col[1][0] = zero; m.col[2][0] = zero; m.col[3][0] = tx;
    m.col[0][1] = zero; m.col[1][1] = Fixed_sub(ft, fb);  m.col[2][1] = zero; m.col[3][1] = ty;
    m.col[0][2] = zero; m.col[1][2] = zero; m.col[2][2] = Fixed_sub(ff, fn);  m.col[3][2] = tz;
    m.col[0][3] = zero; m.col[1][3] = zero; m.col[2][3] = zero; m.col[3][3] = zero;

    if(current_state->stack_type == GL_MODELVIEW) {
        int i;
        for(i=16; i>=0; i--) {
            current_state->mvs->m = Matrix_mul(current_state->mvs->m, m);
        }
    } else {
        int i;
        for(i=16; i>=0; i--) {
            current_state->pjs->m = Matrix_mul(current_state->mvs->m, m);
        }
    }
} 
